package aop;

import Model.*;
import Service.SysInfoService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component("logAspectMain")
public class LogAspectMain {

    @Autowired
    private SysInfoService sysInfoService;

    public Object record(ProceedingJoinPoint point) throws Exception {
        SysInfo sys = new SysInfo();
        try {

            //①point.proceed()函数之前的是：前置通知
            Object result = point.proceed();//目标方法执行后的返回值：msg
            //②point.proceed()函数之后的是：返回通知

//            sys.setOperResult("success");
            System.out.println("seed0");
            setCommonInfo(point, sys);
            System.out.println("-------------->" + sys);

            //根据日志类型进行不同的处理：
            System.out.println("seed1");
            setOtherInfoByTypeId(point, sys, result, sys.getOperTypeId());

            Object localObject2 = result;
            return localObject2;
        } catch (Throwable e) {
            //③异常通知
            e.printStackTrace();
            sys.setOperResult("failure");
        } finally {
            //④后置通知：无论连接点是正常返回还是抛出异常，后置通知都会执行，其实就是写在finally里面
            System.out.println("存放的日志记录-------->" +sys);
            this.sysInfoService.insert(sys);//向数据库保存日志
        }
        return null;
    }

    private void setCommonInfo(ProceedingJoinPoint point, SysInfo sys) {

        System.out.println("seed0-1");
        String methodName = point.getSignature().getName();//切点的方法名
        System.out.println(methodName);
        SysInfoType logType = sysInfoService.getLogTypeContextByMethodName(methodName);//日志类型
        sys.setOperType(logType.getTypeContext());//日志类型
        sys.setOperTypeId(logType.getTypeId());//日志类型Id

        sys.setOperTime(new Date());//时间
        System.out.println("seed0-1-end");
    }


    private void setOtherInfoByTypeId(ProceedingJoinPoint point, SysInfo sys, Object result, int typeId) {
        System.out.println("seed1-1");

        ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder
                .currentRequestAttributes();
        if (attr != null) {
            HttpServletRequest request = attr.getRequest();
            if (request != null) {
                if (typeId == 1) {
                    System.out.println("登陆成功");
                    userSolutionForTypeId_1(point, sys, result);//登陆日志
                } else if (typeId == 2) {
                    System.out.println("界面内的所有操作");
                    userSolutionForTypeId_2(point, result, sys, request);//浏览智能签到
//                } else if(typeId ==3){
//                    System.out.println("监管后台");
//                    userSolutionForTypeId_3(point,result,sys,request);
//                }else if(typeId ==4){
//                    System.out.println("大屏显示");
//                    userSolutionForTypeId_4(point,result,sys,request);
                }
            }
        }else{
            sys.setOperResult("failure");
        }
    }



    private void userSolutionForTypeId_1(ProceedingJoinPoint point, SysInfo sys, Object result) {
        //1：typeId = 1，登陆日志用户设置

        User user = (User) result;//登陆的处理
        System.out.println("1-------------->" + user);
        if (user != null) {
            sys.setUser_id(user.getUser_number());
            sys.setOperator(user.getUser_name());//操作结果：登陆成功
            sys.setOperResult("success");
        } else {
            sys.setOperator(point.getArgs()[0].toString());//登陆失败
            sys.setOperResult("failure");
        }
    }



    private void userSolutionForTypeId_2(ProceedingJoinPoint point, Object result, SysInfo sys, HttpServletRequest request) {

        if (request != null) {
            User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
            System.out.println("2-------------->" + user);
            if (user != null) {
                sys.setUser_id(user.getUser_number());
                sys.setOperator(user.getUser_name());
                sys.setOperResult("success");
            } else {
                sys.setOperator("WITHOUT LOGIN");
            }
        }

    }

}
